package com.netbetter.langzheng.plugin.fileManager.context;

import com.netbetter.langzheng.plugin.fileManager.contents.PasteStatus;
import com.netbetter.langzheng.plugin.fileManager.model.FileModel;
import com.netbetter.langzheng.plugin.fileManager.utils.TreeUtils;

/**
 * @author 嘉陵江里捉鱼 email:tang7259@163.com
 *@version 1.0
 *@time 2008-9-11 下午11:32:10
 */
public class EventUtils {

    /**
     * 判断是否存在需要粘贴的节点
     * 
     * @return
     */
    public static boolean isExistPasteFileModel() {
        if (CopyFileEventContext.getFileModel() != null || CutFileEventContext.getFileModel() != null) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否可进行cut的paste操作
     * 
     * @return
     */
    public static PasteStatus getPasteStatusByCut() {
        FileModel fileModel = CutFileEventContext.getFileModel();
        String oriPath = CutFileEventContext.getCurrentPath();
        FileModel parentFileModel = CutFileEventContext.getParentFileModel();
        if (fileModel != null && parentFileModel != null) {
            return getPasteStatus(parentFileModel, fileModel, oriPath);
        }
        return null;
    }

    /**
     * 判断是否可进行copy的paste操作
     * 
     * @return
     */
    public static PasteStatus getPasteStatusByCopy() {
        FileModel fileModel = CopyFileEventContext.getFileModel();
        String oriPath = CopyFileEventContext.getCurrentPath();
        FileModel parentFileModel = CopyFileEventContext.getParentFileModel();
        if (fileModel != null && parentFileModel != null) {
            return getPasteStatus(parentFileModel, fileModel, oriPath);
        }
        return null;
    }

    /**
     * 判断是否可在当前目录进行粘贴操作
     * 
     * @param fileModel
     * @param oriPath
     * @param parentFileModel
     * @return
     */
    private static PasteStatus getPasteStatus(FileModel parentFileModel, FileModel fileModel, String oriPath) {
        FileModel aimFileModel = ChangeTreeItemEventContext.getFileModel();
        String currentPath = EventUtils.getCurrentFullPath();
        // Log.info("aimFileModel:" + aimFileModel + "");
        // Log.info("parentFileModel:" + parentFileModel + "");
        // Log.info("oriPath:" + oriPath + "");
        // Log.info("currentPath:" + currentPath + "");
        if (parentFileModel.equals(aimFileModel)) {// 父亲目录一致不能粘贴
            return PasteStatus.SAME_PARENT;
        } else {// 目录拷贝到自己子目录将会错误
            if (currentPath.indexOf(oriPath) >= 0) {
                return PasteStatus.IN_SUB_FOLDER;
            }
            // 发现本级目录存在相同名称不能粘贴
            if (TableEventContext.isExistByName(fileModel.getName())) {
                return PasteStatus.SAME_NAME_IN_FOLDER;
            }
        }
        return PasteStatus.NORMAL;
    }

    /**
     * 获得当前节点路径
     * 
     * @return
     */
    public static String getCurrentFullPath() {
        return TreeUtils.getTextPath(ChangeTreeItemEventContext.getTreeItem());
    }
}
